<template>
  <div style="display: flex; justify-content: flex-end">
    <el-form :inline="true">
      <el-form-item>
        <el-input v-model="queryInfo.title" placeholder="标题"></el-input>
      </el-form-item>

      <el-form-item>
        <el-input v-model="queryInfo.info" placeholder="内容"></el-input>
      </el-form-item>

      <el-form-item>
        <el-input v-model="queryInfo.username" placeholder="发布人"></el-input>
      </el-form-item>

      <el-form-item>
        <el-button-group>
          <el-button type="danger" @click="resetQuery">重置</el-button>
          <el-button type="primary" @click="requery">查询</el-button>
          <el-button type="warning" @click="showadd">添加</el-button>
        </el-button-group>
      </el-form-item>
    </el-form>
  </div>

  <!-- {{ list }} -->
  <!-- 数据呈现的部分 -->
  <div>
    <el-table v-loading="loading" :data="list" stripe border="true">
      <el-table-column label="标题" prop="title"></el-table-column>
      <el-table-column label="发布人" prop="username"></el-table-column>
      <el-table-column
        width="160"
        label="是否置顶"
        prop="topIndex"
      ></el-table-column>
      <el-table-column label="公告发布时间">
        <template #default="scope">
          {{ tools.formatDate(scope.row.lastupdate) }}
        </template>
      </el-table-column>
      <el-table-column label="操作">
        <template #default="scope">
          <el-button @click="del(scope.row)" type="danger" size="smail"
            >删除</el-button
          >
          <el-button @click="showDetail(scope.row.bid)" type="info" size="smail"
            >查看内容</el-button
          >
          <el-button
            @click="showModify(scope.row.bid)"
            type="success"
            size="smail"
            >修改</el-button
          >
        </template>
      </el-table-column>
    </el-table>
  </div>
  <!-- 分页的部分 -->
  <div>
    <el-pagination
      v-model:current-page="page.pageNumber"
      :page-size="page.pageSize"
      :total="page.total"
      layout="prev, pager, next,total"
      @current-change="query"
    >
    </el-pagination>
  </div>
  <!-- 添加的对话框 -->
  <el-dialog v-model="avisible" title="发布公告" @closed="resetQuery">
    <div>
      <el-form>
        <el-form-item>
          <el-input v-model="addInfo.title" placeholder="标题" />
        </el-form-item>
        <el-form-item>
          <el-input v-model="addInfo.username" placeholder="发布人" />
        </el-form-item>
        <el-form-item>
          <el-input
            rows="10"
            type="textarea"
            v-model="addInfo.content"
            placeholder="公告内容"
          />
        </el-form-item>
      </el-form>
    </div>
    <template #footer>
      <el-button type="danger" @click="avisible = false">关闭</el-button>
      <el-button type="warning" @click="add">发布</el-button>
    </template>
  </el-dialog>

  <!-- 查看内容的对话框 -->
  <el-dialog v-model="dvisible" :title="detail.title" fullscreen>
    <div>
      <div class="detail" v-html="detail.content"></div>
      <div>
        {{ tools.formatDate(detail.lastupdate) }}
        {{ detail.source }}
      </div>
    </div>
  </el-dialog>

  <!-- 修改的对话框 -->
  <el-dialog v-model="mvisible" title="修改公告" @closed="resetQuery">
    <div>
      <el-form>
        <el-form-item>
          <el-input v-model="modifyInfo.title" placeholder="标题" />
        </el-form-item>
        <el-form-item>
          <el-input v-model="modifyInfo.username" placeholder="发布人" />
        </el-form-item>
        <el-form-item>
          <el-input
            rows="10"
            type="textarea"
            v-model="modifyInfo.content"
            placeholder="内容"
          />
        </el-form-item>
      </el-form>
    </div>
    <template #footer>
      <el-button type="danger" @click="mvisible = false">关闭</el-button>
      <el-button type="warning" @click="modify">保存</el-button>
    </template>
  </el-dialog>
</template>
<script setup>
import { ref } from 'vue'
import { api } from '../../js/ajax'
import { tools } from '../../js/tools'
import { ElMessageBox, ElMessage } from 'element-plus'

const loading = ref(false)

//#region 查询部分
const resetQuery = () => {
  queryInfo.value = {
    title: '',
    content: '',
    username: '',
  }
  query()
}

const requery = () => {
  page.value.pageNumber = 1
  query()
}
const page = ref({
  pageNumber: 1,
  pageSize: 10,
})
const queryInfo = ref({
  title: '',
  content: '',
  username: '',
})
const list = ref([])

const query = () => {
  loading.value = true
  api.get(
    '/crud/billboard',
    tools.mergeJson(queryInfo.value, page.value),
    data => {
      setTimeout(() => {
        loading.value = false
      }, 0)

      if (data.success) {
        list.value = data.data.data
        page.value = data.data.pageBean
        return
      }
      ElMessageBox.alert(data.message, '新闻管理')
    },
  )
}
query()
//#endregion

//#region  添加的部分
const avisible = ref(false)

const addInfo = ref({
  title: '',
  content: '',
  username: '',
})

const showadd = () => {
  addInfo.value = {
    title: '',
    content: '',
    username: '',
  }
  avisible.value = true
}
const add = () => {
  api.post('/crud/billboard', addInfo.value, data => {
    if (data.success) {
      ElMessage.success('发布成功')
      addInfo.value = {
        title: '',
        content: '',
        username: '',
      }
      return
    }
    ElMessageBox.alert(data.message, '发布公告')
  })
}

//#endregion

//#region  查看内容的部分
const detail = ref({
  bid: -1,
  username: '',
  title: '',
  content: '',
  topIndex: 0,
  lastupdate: '',
})
const dvisible = ref(false)

const showDetail = bid => {
  loading.value = true
  console.log('公告id:', bid)
  api.get(`/crud/billboard/${bid}`, {}, data => {
    loading.value = false
    if (data.success) {
      detail.value = data.data
      dvisible.value = true
      return
    }
    ElMessageBox.alert(data.message, '公告管理')
  })
}

//#endregion

//#region 修改的部分

const modifyInfo = ref({
  bid: 1,
  username: '',
  title: '',
  content: '',
  topIndex: 10000,
  lastupdate: '',
})
const mvisible = ref(false)

const showModify = bid => {
  loading.value = true
  api.get(`/crud/billboard/${bid}`, {}, data => {
    loading.value = false
    if (data.success) {
      modifyInfo.value = data.data
      mvisible.value = true
      return
    }
    ElMessageBox.alert(data.message, '公告管理')
  })
}

const modify = () => {
  api.put(`/crud/billboard`, modifyInfo.value, data => {
    ElMessageBox.alert(data.message, '公告管理')
  })
}

//#endregion

//#region 删除的功能
const del = content => {
  console.log('要删除的信息：', content)
  ElMessageBox.confirm(`是否删除公告信息：${content.title}?`, '删除信息')
    .then(() => {
      api.delete(`/crud/billboard/${content.bid}`, {}, data => {
        ElMessage(data.message)
        requery()
      })
    })
    .catch(() => {})
}

//#endregion
</script>

<style scoped>
.detail {
  /* max-height: 60vh; */
  overflow-y: auto;
}
.detail:deep img {
  display: block;
  width: 100%;
  height: auto;
}
</style>
